package com.boost.socialNetwork.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.Constants;

import com.reveregroup.gwt.facebook4gwt.FacebookStory;


public class IdeaPage extends FlexTable {

	private String ideaKey;
	private String userKey;
	private final UserServiceAsync greetService = GWT
			.create(UserService.class);
	private FlexTable Comments_table;
	private TextArea CommentTextBox;
	private Button Submit_Comment;
	private UserServiceAsync service = GWT.create(UserService.class);
	private String[] array;
	private Button btnDelete;

	public IdeaPage(String idea, boolean canDelete) {
 	greetService.getConnected(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
			Window.alert("Error");
				
			}

			@Override
			public void onSuccess(String result) {
				if(result==null)
				HistoryManager.newHistory("PageLogin");				
			}
		});

		System.out.println("Da5al el Idea");
		//HistoryManager.newHistory("PageIdea");
		String[] a = idea.split(",");
		FlexTable flexTable = new FlexTable();
		setWidget(0, 0, flexTable);
		// add(flexTable);
		// setWidgetLeftWidth(flexTable, 46.0, Unit.PX, 415.0, Unit.PX);
		// setWidgetTopHeight(flexTable, 33.0, Unit.PX, 275.0, Unit.PX);

		ideaKey = a[0];

		Label creator = new Label();
		creator.setText("Creator: " + a[1]);
		creator.setStyleName("gwt-regLabel");
		flexTable.setWidget(0, 0, creator);

		Label date = new Label();
		date.setText("Created on: " + a[2]);
		date.setStyleName("gwt-regLabel");
		flexTable.setWidget(0, 1, date);

		Label title = new Label();
		title.setText("Title: " + a[3]);
		title.setStyleName("gwt-regLabel");
		flexTable.setWidget(1, 0, title);

		Label description = new Label("Description: ");
		description.setStyleName("gwt-regLabel");
		flexTable.setWidget(2, 0, description);

		RichTextArea desc = new RichTextArea();
		desc.setEnabled(false);
		desc.setText(a[4]);
		flexTable.setWidget(3, 1, desc);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_RIGHT);

		// LayoutPanel layoutPanel = new LayoutPanel();
		// add(layoutPanel);
		// setWidgetLeftWidth(layoutPanel, 46.0, Unit.PX, 491.0, Unit.PX);
		// setWidgetTopHeight(layoutPanel, 279.0, Unit.PX, 240.0, Unit.PX);
		// setWidget(1,0, layoutPanel);

		service.getConnected(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error");

			}

			@Override
			public void onSuccess(String result) {

				userKey = result;

			}

		});

		// FlexTable flexTable_1 = new FlexTable();
		// layoutPanel.add(flexTable_1);
		// layoutPanel.setWidgetLeftWidth(flexTable_1, 113.0, Unit.PX, 254.0,
		// Unit.PX);
		// layoutPanel.setWidgetTopHeight(flexTable_1, 30.0, Unit.PX, 167.0,
		// Unit.PX);
		//
		// setWidget(1,0,flexTable_1);

		Comments_table = new FlexTable();
		Comments_table.setStyleName("gwt-date");
		setWidget(1, 0, Comments_table);

		Comments_table.setText(0, 0, "Date");
		Comments_table.setText(0, 1, "Author");
		Comments_table.setText(0, 2, "Comment");

		CommentTextBox = new TextArea();
		CommentTextBox.addMouseDownHandler(new MouseDownHandler() {
			public void onMouseDown(MouseDownEvent event) {
				CommentTextBox.setText("");
				CommentTextBox.setStyleName(".gwt-TextBox");
			}
		});
		
		Button btnShareOnFacebook = new Button("Share on Facebook");
		btnShareOnFacebook.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				FacebookStory story = new FacebookStory();
				 story.putData("name", "title"); //you can use {*actor*} which uses the logged users name
				 story.putData("caption", "tehasdsasdf");
				 story.putData("description", "tehasdsasdf");
				 story.putData("href", "http://www.inasbook.appspot.com");//adds href for 'name
				// story.addImage("http://i.cdn.turner.com/cnn/.element/img/3.0/global/header/hdr-main.gif", "http://www.cnn.com");
				 story.setActionLinkText("another link to something");
				 story.setActionLinkHref("http://www.google.com");

				story.streamPublish();
				
//				FacebookStory fbStory = new FacebookStory();
//				fbStory.setTemplateBundleId("");
//				fbStory.setPrompt("Add your thoughts if you like...");
//				fbStory.putData("headline", "has shared some Ideas on inasbook.appspot.com");
//				fbStory.putData("story_url", "inasbook.appspot.com");
//				fbStory.setBodyGeneral("On inasbook you can post and comment on a varity of ideas fakes face now inasbook.");				
//				fbStory.showFeedDialog();
			}
		});
		setWidget(1, 1, btnShareOnFacebook);
		setWidget(2, 0, CommentTextBox);
		CommentTextBox.setStyleName("gwt-shaded_Text");
		CommentTextBox.setText("Write Your Comment");

		Submit_Comment = new Button("Submit");
		Submit_Comment.setStyleName("gwt-comment");
		setWidget(2, 1, Submit_Comment);
		Submit_Comment.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				if (!CommentTextBox.getText().equals("")
						&& !CommentTextBox.getText().equals(
								"Write Your Comment")) {

					service.commentOnIdea( ideaKey, userKey, CommentTextBox.getText(),
							 new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									Window.alert("Error");

								}

								@Override
								public void onSuccess(String result) {
									fillTable();

								}

							});

				}

			}
		});
		String html=("<form action='https://www.paypal.com/cgi-bin/webscr' method='post'>"
				 + "<input type='hidden' name='cmd' value='_s-xclick'>"
				 + "<input type='hidden' name='hosted_button_id' value='UCFYZTPBZA84N'>"
				 + "<input type='image' src='https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'>"
				 + "<img alt='' border='0' src='https://www.paypalobjects.com/en_US/i/scr/pixel.gif' width='1' height='1'>"
				 + "</form>");
		flexTable.setWidget(4, 0,new HTMLPanel(html) );
		if (canDelete) {
			// Button btnDelete = new Button("Delete");
			btnDelete = new Button("Delete Idea");
			flexTable.setWidget(3, 0, btnDelete);

			// setWidgetLeftWidth(btnDelete, 344.0, Unit.PX, 81.0, Unit.PX);
			// setWidgetTopHeight(btnDelete, 328.0, Unit.PX, 30.0, Unit.PX);
			btnDelete.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					boolean x = Window
							.confirm("Are you sure you want to delete this idea?");
					if (x) {
						greetService.deleteIdea(ideaKey,
								new AsyncCallback<String>() {

									@Override
									public void onSuccess(String result) {
										if (result.equals("deleted")) {
											Window.alert("Idea Deleted");
//											RootPanel.get("post").clear();
//											RootPanel.get("head").clear();
//											Home home = new Home();
//											RootPanel.get("post").add(home);
//											RootPanel.get("head").add(
//													home.getHead());
											HistoryManager.newHistory("PageHome");
										}
									}

									@Override
									public void onFailure(Throwable caught) {
										// TODO Auto-generated method stub

									}
								});

					}

				}
			});
		}

		array = idea.split(",");
		fillTable();
	
/*		HistoryManager.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				HistoryManager.HistoryChange(event);

			}
		});*/

	}

	public void fillTable() {

		String ideaID = array[0];
		Comments_table.clear();

		service.retrieveComments(ideaID,
				new AsyncCallback<List<String>>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error");

					}

					@Override
					public void onSuccess(List<String> result) {

						if (result.size() == 0)
							Comments_table.setVisible(false);
						else {
							Comments_table.setVisible(true);
							for (int i = 0; i < result.size(); i++) {
								String[] contents = result.get(i).split(",");
								Label comment = new Label(contents[3]);
								comment.setStyleName("gwt-comment");
								Label Date = new Label(contents[2]);
								Date.setStyleName("gwt-date");
								Label Author = new Label(contents[1]);
								Author.setStyleName("gwt-date");

								Comments_table.setWidget(i + 1, 0, Date);
								Comments_table.setWidget(i + 1, 1, Author);
								Comments_table.setWidget(i + 1, 2, comment);

							}

						}

					}
				});

	}
}
